package cn.green.core.adapt.comps.field.type;

import cn.green.core.adapt.models.field.OrmField;
import cn.green.core.common.enums.field.ColumnType;
import cn.green.core.components.entity.ColumnLength;
import cn.green.core.components.entity.JoinInfo;
import cn.green.core.components.entity.QCondition;
import cn.green.core.components.field.FieldTypeCom;
import cn.green.core.excel.model.Column;
import cn.green.core.front.model.Fetch;
import cn.green.core.front.model.Input;
import cn.green.core.front.model.Title;
import cn.green.core.model.QFieldModel;
import cn.green.core.model.QTableModel;
import cn.green.core.scanner.anno.comps.QTableComponent;
import cn.green.manage.cache.QCache;
import cn.green.utils.OrmAppUtils;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author wang.yishu
 * @version 1.0
 * @className OrmColumnType
 * @description orm的字段类型,根据@QColumn的注解所配置,该字段类型为实体类型，均在数据表中存在
 * @date 2025/5/14 14:12
 */
@QTableComponent
public class OrmColumnType implements FieldTypeCom {
    public static final String TYPE_CODE = "ORM_COLUMN_TYPE";
    @Override
    public String fieldTypeName() {
        return "OrmColumnType";
    }

    @Override
    public String fieldTypeCode() {
        return TYPE_CODE;
    }

    @Override
    public ColumnType getColumnType(QFieldModel fieldModel) {
        if (fieldModel instanceof OrmField) {
            return ((OrmField) fieldModel).getColumnType();
        }
        return null;
    }

    @Override
    public ColumnLength getColumnLength(QFieldModel fieldModel) {
        if (fieldModel instanceof OrmField) {
            int length = ((OrmField) fieldModel).getLength();
            int digits = ((OrmField) fieldModel).getDigits();
            return new ColumnLength(length, digits);
        }
        return null;
    }

    @Override
    public boolean canBeSelected() {
        return false;
    }


    @Override
    public void formatRecord(JSONObject record, QFieldModel field) {
        //todo 格式化数据 需要开发
    }

    @Override
    public void parseRecord(JSONObject record, QFieldModel field) {
    }

    @Override
    public List<Input> inputs(QFieldModel field) {
        return Collections.emptyList();
    }

    @Override
    public List<Title> titles(QFieldModel field) {
        return Collections.emptyList();
    }

    @Override
    public List<Fetch> fetches(QTableModel table, QFieldModel field) {
        return Collections.emptyList();
    }

    @Override
    public List<QCondition> conditions(QFieldModel field, JSONObject param) {
        String fieldName = field.getFieldName();
        String sourceName = field.getSourceName();
        String schemeName = field.getSchemeName();
        String tableName = field.getTableName();
        QCache qCache = OrmAppUtils.getBean(QCache.class);
        QTableModel tableFromCache = qCache.getTableFromCache(sourceName, schemeName, tableName);
        if (param.containsKey(fieldName)) {
            QCondition qCondition = new QCondition();
            qCondition.setConditionTable(tableFromCache);
            qCondition.setConditionField(field);
            qCondition.setType(QCondition.ConditionType.EQ);
            List<Object> values = new ArrayList<>();
            values.add(param.get(fieldName));
            qCondition.setValues(values);
            return Collections.singletonList(qCondition);
        }
        return Collections.emptyList();
    }

    @Override
    public List<JoinInfo> joinInfos(QTableModel table, QFieldModel field) {
        return Collections.emptyList();
    }

    @Override
    public List<Column> columns(QFieldModel field) {
        return Collections.emptyList();
    }
}
